package Yangtz.Cs.Utils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

public class DataTransmissionUtil {
    public static JSONObject gainJsonFromAndroid(HttpServletRequest request) throws IOException {
        InputStream in = request.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(in,"utf-8"));
        String str = br.readLine();//这时接收的便是josn格式的字符串
        JSONObject ob = JSONObject.fromObject(str);//将json字符串转换为对象，然后可以通过key来取值了
        return ob;
    }

    public static void sendJsonToAndroid(JSONObject jsonObject, HttpServletResponse response) throws IOException {
        OutputStream os = response.getOutputStream();
        os.write(jsonObject.toString().getBytes("utf-8"));
    }

    public static void sendJsonArrayToAndroid(JSONArray jsonArray, HttpServletResponse response) throws IOException {
        OutputStream os = response.getOutputStream();
        os.write(jsonArray.toString().getBytes("utf-8"));
    }
}
